<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>订单管理系统</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/my_css.css">
</head>

<body>
    <blockquote class="layui-elem-quote layui-text">
        更新库存
    </blockquote>

    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-block">
                <input type="text" name="product_name" autocomplete="off"
                       class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">添加数量</label>
            <div class="layui-input-block">
                <input type="number" name="product_num" autocomplete="off"
                       class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="update_stock">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</body>
<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
    layui.use('form', function() {
        let form = layui.form;
        form.on('submit(update_stock)', function (data) {
            let productName = data.field.product_name;
            let productNum = data.field.product_num;
            $.ajax({
                url: 'update_stock',
                data: {
                    'productName': productName,
                    'productNum': parseInt(productNum)
                },
                method: 'post',
                success: function (res) {
                    $(':input').val('');
                    if (res) {
                        layer.msg('更新库存成功!', {
                            icon: 1,
                            time: 2000
                        });
                    }
                },
                error: function () {
                    layer.msg('更新库存错误!', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
            return false;
        });
    });
</script>

</html>